package com.chookss.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.JavascriptInterface;

import com.chookss.HomeActivityss;
import com.chookss.home.NavigationBaseActivity;
import com.chookss.home.publish.PublishLogsActivity;
import com.chookss.home.publish.PublishNewsActivity;
import com.chookss.tiku.ExamRecordListActivity;
import com.chookss.tiku.PracticeTestActivity;
import com.chookss.tiku.SelectPracticeCatalog;
import com.chookss.tiku.SelectSimulationCatalog;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.PermissionSpUtils;
import com.chookss.video.VideoBaseActivity;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class X5WebView extends WebView {
    private Context mContext;
    private WebViewClient client = new WebViewClient() {
        /**
         * 防止加载网页时调起系统浏览器
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    };

    @SuppressLint("SetJavaScriptEnabled")
    public X5WebView(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
        this.mContext = arg0;
        this.setWebViewClient(client);
        // this.setWebChromeClient(chromeClient);
        // WebStorage webStorage = WebStorage.getInstance();
        initWebViewSettings();
        this.getView().setClickable(true);
    }

    private void initWebViewSettings() {
        WebSettings webSetting = this.getSettings();
        webSetting.setDefaultTextEncodingName("utf-8");
        webSetting.setJavaScriptEnabled(true);
        addJavascriptInterface(new JavaScriptInterface(), "android");
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowContentAccess(true); // 是否可访问Content Provider的资源，默认值 true
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
        setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);

        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            webSetting.setAllowFileAccessFromFileURLs(false);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            webSetting.setMixedContentMode(0);
        }
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
//        setDownloadListener(new MyWebViewDownLoadListener());
        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
        // settings 的设计
    }

//    class MyWebViewDownLoadListener implements DownloadListener {
//
//        @Override
//        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
//                                    long contentLength) {
//            Intent intent = new Intent(Intent.ACTION_VIEW);
//            intent.addCategory(Intent.CATEGORY_BROWSABLE);
//            intent.setData(Uri.parse(url));
//            mContext.startActivity(intent);
//        }
//    }

    class JavaScriptInterface {
        @JavascriptInterface
        public void title(String title) {
            KLog.i(title + "===============");
        }

        @JavascriptInterface
        public void closeLoading() {
            EventBus.getDefault().post(new MyEvent("Web_Cancel_dialog"));
        }

        /**
         * 跳转到原生
         */
        @JavascriptInterface
        public void jumpToApp(String type) {
            Intent intent = null;
            switch (type) {
                case "1":
                    //通告
                    if (PermissionSpUtils.getBoolean(mContext,"01",false)) {
                        String code1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "");
                        String code3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "");
                        if ("01".equals(code1) || "01".equals(code3)) {
                            intent = new Intent(mContext, HomeActivityss.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            Map<String,String> map=new HashMap<>();
                            map.put("HomeBottom","Gonggao");
                            map.put("code","01");
                            EventBus.getDefault().post(new MyEvent(map));
                            EventBus.getDefault().post(new MyEvent("Web_Close"));
                        } else {
                            intent = new Intent(mContext, NavigationBaseActivity.class);
                            intent.putExtra("type",1);
                            mContext.startActivity(intent);
                        }

                    } else {
                        MyToast.show("管理员已关闭公告功能");
                    }
                    break;
                case "2":
                    //阅读新闻
                    intent = new Intent(mContext, HomeActivityss.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                    EventBus.getDefault().post(new MyEvent("HomeBottom0"));
                    EventBus.getDefault().post(new MyEvent("scrollToNews"));
                    EventBus.getDefault().post(new MyEvent("Web_Close"));
                    break;
                case "3":
                    //发布新闻
                    if (PermissionSpUtils.getBoolean(mContext,"11",false)) {
                        intent = new Intent(mContext, PublishNewsActivity.class);
                        mContext.startActivity(intent);
                    } else {
                        MyToast.show("管理员已关闭新闻发布功能");
                    }
                    break;
                case "4":
                    //发布日志
                    if (PermissionSpUtils.getBoolean(mContext,"12",false)) {
                        intent = new Intent(mContext, PublishLogsActivity.class);
                        intent.putExtra("type", 3);
                        mContext.startActivity(intent);
                    } else {
                        MyToast.show("管理员已关闭日志发布功能");
                    }
                    break;
                case "5":
                    //查看日志
                    if (PermissionSpUtils.getBoolean(mContext,"02",false)) {
                        String code1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "");
                        String code3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "");
                        if ("02".equals(code1) || "02".equals(code3)) {
                            intent = new Intent(mContext, HomeActivityss.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            Map<String,String> map=new HashMap<>();
                            map.put("HomeBottom","Rizhi");
                            map.put("code","02");
                            EventBus.getDefault().post(new MyEvent(map));
                            EventBus.getDefault().post(new MyEvent("Web_Close"));
                        } else {
                            intent = new Intent(mContext, NavigationBaseActivity.class);
                            intent.putExtra("type",2);
                            mContext.startActivity(intent);
                        }

                    } else {
                        MyToast.show("管理员已关闭日志功能");
                    }
                    break;
                case "6":
                    //观看小视频
                    if (PermissionSpUtils.getBoolean(mContext,"14",false)) {
                        String code1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "");
                        String code3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "");
                        if ("14".equals(code1) || "14".equals(code3)|| "19".equals(code1)|| "19".equals(code3)) {
                            intent = new Intent(mContext, HomeActivityss.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            EventBus.getDefault().post(new MyEvent("HomeVedioBottom1"));
                            //定时器 倒计时
                            Observable.timer(400, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<Long>() {
                                        @Override
                                        public void accept(Long aLong) {
                                            try {
                                                EventBus.getDefault().post(new MyEvent("HomeVedio1"));
                                                EventBus.getDefault().post(new MyEvent("Web_Close"));
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                        } else {
                            intent = new Intent(mContext, VideoBaseActivity.class);
                            intent.putExtra("index",1);
                            mContext.startActivity(intent);
                        }

                    } else {
                        MyToast.show("管理员已关闭视频功能");
                    }
                    break;
                case "7":
                    //观看课程
                    if (PermissionSpUtils.getBoolean(mContext,"14",false)) {
                        String code1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "");
                        String code3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "");
                        if ("14".equals(code1) || "14".equals(code3)|| "19".equals(code1)|| "19".equals(code3)) {
                            intent = new Intent(mContext, HomeActivityss.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            EventBus.getDefault().post(new MyEvent("HomeVedioBottom2"));
                            EventBus.getDefault().post(new MyEvent("HomeVedio2"));
                            EventBus.getDefault().post(new MyEvent("Web_Close"));
                        } else {
                            intent = new Intent(mContext, VideoBaseActivity.class);
                            mContext.startActivity(intent);
                        }
                    } else {
                        MyToast.show("管理员已关闭视频功能");
                    }
                    break;
                case "8":
                    //智能练习
                    if (PermissionSpUtils.getBoolean(mContext,"13",false)) {
                        intent = new Intent(mContext, PracticeTestActivity.class);
                        intent.putExtra("practiceType", "1");
                        mContext.startActivity(intent);
                    } else {
                        MyToast.show("管理员已关闭智能练习功能");
                    }
                    break;
                case "9":
                    //专项练习
                    if (PermissionSpUtils.getBoolean(mContext,"13",false)) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent = new Intent(mContext, SelectPracticeCatalog.class);
                            mContext.startActivity(intent);
                        }

                    } else {
                        MyToast.show("管理员已关闭专项练习功能");
                    }
                    break;
                case "10":
                    //模拟卷
                    if (PermissionSpUtils.getBoolean(mContext,"13",false)) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent = new Intent(mContext, SelectSimulationCatalog.class);
                            mContext.startActivity(intent);
                        }

                    } else {
                        MyToast.show("管理员已关闭模拟卷功能");
                    }
                    break;
                case "11":
                    //阅读文档
                    if (PermissionSpUtils.getBoolean(mContext,"03",false)) {
                        String code1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "");
                        String code3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "");
                        if ("03".equals(code1) || "03".equals(code3)) {
                            intent = new Intent(mContext, HomeActivityss.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            Map<String,String> map=new HashMap<>();
                            map.put("HomeBottom","Document");
                            map.put("code","03");
                            EventBus.getDefault().post(new MyEvent(map));
                            EventBus.getDefault().post(new MyEvent("Web_Close"));
                        } else {
                            intent = new Intent(mContext, NavigationBaseActivity.class);
                            intent.putExtra("type",3);
                            mContext.startActivity(intent);
                        }
                    } else {
                        MyToast.show("管理员已关闭资料库功能");
                    }
                    break;
                case "12":
                    //发布原创视频
                    if (PermissionSpUtils.getBoolean(mContext,"10",false)) {
                        intent = new Intent(mContext, HomeActivityss.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                        EventBus.getDefault().post(new MyEvent("HomeRecord"));
                        EventBus.getDefault().post(new MyEvent("Web_Close"));
                    } else {
                        MyToast.show("管理员已关闭视频发布功能");
                    }
                    break;
                case "13":
                    //发布非原创视频
                    if (PermissionSpUtils.getBoolean(mContext,"10",false)) {
                        intent = new Intent(mContext, HomeActivityss.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                        EventBus.getDefault().post(new MyEvent("HomeRecord"));
                        EventBus.getDefault().post(new MyEvent("Web_Close"));
                    } else {
                        MyToast.show("管理员已关闭视频发布功能");
                    }
                    break;
                case "14":
                    //题目被练习
                    if (PermissionSpUtils.getBoolean(mContext,"13",false)) {
                        String code1 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_1_CODE, "");
                        String code3 = new ShareUtils().getString(mContext, StaticClass.BOTTOM_3_CODE, "");
                        if ("13".equals(code1) || "13".equals(code3)) {
                            intent = new Intent(mContext, HomeActivityss.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            EventBus.getDefault().post(new MyEvent("HomeBottom_Tiku"));
                            EventBus.getDefault().post(new MyEvent("Web_Close"));
                        } else {
                            intent = new Intent(mContext, NavigationBaseActivity.class);
                            intent.putExtra("type",0);
                            mContext.startActivity(intent);
                        }
                    } else {
                        MyToast.show("管理员已关闭题库功能");
                    }
                    break;
                case "15":
                    //答题记录
                    if (PermissionSpUtils.getBoolean(mContext,"13",false)) {
                        intent = new Intent(mContext, ExamRecordListActivity.class);
                        mContext.startActivity(intent);
                    } else {
                        MyToast.show("管理员已关闭题库功能");
                    }
                    break;
                case "16":
                    //登录
                    EventBus.getDefault().post(new MyEvent("Web_Close"));
                    break;
            }

        }
    }
}